import {
  addPlace,
  modifyPlace,
  deletePlace,
  findPlaceForGame,
  addZubiePlace,
  modifyZubiePlace,
  deleteZubiePlace,
  findTimeForGame,
  findPlaceColumns,
  findPlaceData,
  patchAddZubiePlace,
  findZhixuForKongchang,
  findGameCaipan,
  findCaipanByTel,
  addCaipan,
  deleteCaipan,
  setCaipan,
  queryMatchCaipan,
} from '../services/place';

import { message } from 'antd';

export default {
  namespace: 'place',

  state: {
    placeList: [],
    zubiePlaceList: [],
    placeColumns: [],
    placeData: [],
    kongchangList: [],
    caipanList: [],
    curCaipan: {},
    matchCaipanList: [],
  },

  effects: {
    *addPlace({ payload, callback }, { select, call, put }) {
      const response = yield call(addPlace, payload);
      if (response) {
        yield put({
          type: 'findPlaceForGame',
        });
        if (callback) callback();
      }
    },
    *modifyPlace({ payload, callback }, { select, call, put }) {
      const response = yield call(modifyPlace, payload);
      if (response) {
        yield put({
          type: 'findPlaceForGame',
        });
        if (callback) callback();
      }
    },
    *deletePlace({ payload, callback }, { select, call, put }) {
      const response = yield call(deletePlace, payload);
      if (response) {
        yield put({
          type: 'findPlaceForGame',
        });
        if (callback) callback();
      }
    },
    *findPlaceForGame({ payload }, { select, call, put }) {
      const curGame = yield select(state => state.gameList.modifyItem);
      const response = yield call(findPlaceForGame, curGame.code);
      if (response) {
        yield put({
          type: 'changeState',
          payload: {
            placeList: Array.isArray(response) ? response : [],
          },
        });
      }
    },
    *findTimeForGame({ payload }, { select, call, put }) {
      const response = yield call(findTimeForGame, payload);
      if (response) {
        yield put({
          type: 'changeState',
          payload: {
            zubiePlaceList: Array.isArray(response) ? response : [],
          },
        });
      }
    },
    *findPlaceColumns({ payload }, { select, call, put }) {
      const response = yield call(findPlaceColumns, payload);
      if (response) {
        yield put({
          type: 'changeState',
          payload: {
            placeColumns: Array.isArray(response) ? response : [],
          },
        });
      }
    },
    *findPlaceData({ payload }, { select, call, put }) {
      const response = yield call(findPlaceData, payload);
      if (response) {
        yield put({
          type: 'changeState',
          payload: {
            placeData: Array.isArray(response) ? response : [],
          },
        });
      }
    },
    *findZhixuForKongchang({ payload }, { select, call, put }) {
      const response = yield call(findZhixuForKongchang, payload);
      if (response) {
        yield put({
          type: 'changeState',
          payload: {
            kongchangList: Array.isArray(response) ? response : [],
          },
        });
      }
    },
    *findGameCaipan({ payload }, { select, call, put }) {
      const curGame = yield select(state => state.gameList.modifyItem);
      const response = yield call(findGameCaipan, curGame.code);
      if (response) {
        yield put({
          type: 'changeState',
          payload: {
            caipanList: Array.isArray(response) ? response : [],
          },
        });
      }
    },
    *findCaipanByTel({ payload }, { select, call, put }) {
      const response = yield call(findCaipanByTel, payload);
      if (response) {
        if (response.id) {
          yield put({
            type: 'changeState',
            payload: {
              curCaipan: response,
            },
          });
        } else {
          message.warn('没有找到裁判信息');
        }
      }
    },
    *addCaipan({ payload, callback }, { select, call, put }) {
      const curGame = yield select(state => state.gameList.modifyItem);
      payload.gameCode = curGame.code;
      const response = yield call(addCaipan, payload);
      if (response) {
        if (callback) callback();
      }
    },
    *deleteCaipan({ payload, callback }, { select, call, put }) {
      const curGame = yield select(state => state.gameList.modifyItem);
      payload.gameCode = curGame.code;
      const response = yield call(deleteCaipan, payload);
      if (response) {
        if (callback) callback();
      }
    },
    *setCaipan({ payload, callback }, { select, call, put }) {
      const response = yield call(setCaipan, payload);
      if (response) {
        if (callback) callback();
      }
    },
    *queryMatchCaipan({ payload, callback }, { select, call, put }) {
      const response = yield call(queryMatchCaipan, payload);
      if (response) {
        yield put({
          type: 'changeState',
          payload: {
            matchCaipanList: response,
          },
        });
        if (callback) callback();
      }
    },
  },

  reducers: {
    changeState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
